package leetcode.tree;

/**
 *
 * @author MaoLin Wang
 * @date 2020/2/1417:18
 */
public class _101IsSymmetric {
    public  static boolean isSymmetric(TreeNode root) {
        if (root==null){
            return true;
        }
        if (root.left==null&&root.right==null){
            return true;
        }
        if ((root.left!=null&&root.right==null)||(root.left==null&&root.right!=null)){
            return false;
        }
        if (root.left.val!=root.right.val){
            return false;
        }
        return compare(root.left,root.right);
    }
    private static boolean compare(TreeNode p, TreeNode q) {

        while (p!=null&&q!=null){
            if (p.val==q.val){
                boolean left = compare(p.left, q.right);
                boolean right = compare(p.right, q.left);
                return left&&right;
            }else {
                return false;
            }
        }
        if (p==null&&q==null){
            return true;
        }else {
            return false;
        }
    }

    public static void main(String[] args) {
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.left.left=new TreeNode(3);
        root.left.right=new TreeNode(4);
        root.right=new TreeNode(2);
        root.right.left=new TreeNode(4);
        root.right.right=new TreeNode(3);
        boolean symmetric = isSymmetric(root);
        System.out.println(symmetric);
    }
}
